﻿// Extensions Made Easy - Copyright 2012 by Jan Van der Haegen - All rights reserved, use for educational purposes only.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using ExtensionsMadeEasy.ClientAPI.Navigation;
using ExtensionsMadeEasy.ClientAPI.Utilities.Base;
using Microsoft.VisualStudio.ExtensibilityHosting;

namespace ExtensionsMadeEasy.Navigation
{
    [Export(typeof(IEasyNavigationModel))]
    public sealed class EasyNavigationModel : NotifyPropertyChangedBase, IEasyNavigationModel
    {
        private readonly IScreenOpener screenOpener;
        private readonly IEntityOpener entityOpener;
        private readonly IRecentlySelectedScreenNavigator selectedScreenNavigator;
        private readonly IRecentlyOpenedScreenListener recentlyOpenedScreenListener;
        private readonly IRecentlyClosedScreenListener recentlyClosedScreenListener;
        private readonly IFavoriteOpenedScreenListener favoriteClosedScreenListener;

        public EasyNavigationModel()
        {
            this.screenOpener = VsExportProviderService.GetExportedValue<IScreenOpener>();
            this.entityOpener = VsExportProviderService.GetExportedValue<IEntityOpener>();
            this.selectedScreenNavigator = VsExportProviderService.GetExportedValue<IRecentlySelectedScreenNavigator>();
            this.selectedScreenNavigator.PropertyChanged += new PropertyChangedEventHandler(innerPropertyChanged);
            try
            {
                this.recentlyOpenedScreenListener = VsExportProviderService.GetExportedValue<IRecentlyOpenedScreenListener>();
                this.recentlyOpenedScreenListener.PropertyChanged += new PropertyChangedEventHandler(innerPropertyChanged);

                this.recentlyClosedScreenListener = VsExportProviderService.GetExportedValue<IRecentlyClosedScreenListener>();
                this.recentlyClosedScreenListener.PropertyChanged += new PropertyChangedEventHandler(innerPropertyChanged);

                this.favoriteClosedScreenListener = VsExportProviderService.GetExportedValue<IFavoriteOpenedScreenListener>();
                this.favoriteClosedScreenListener.PropertyChanged += new PropertyChangedEventHandler(innerPropertyChanged);
            }
            catch (Exception) 
            {
            }
        }

        public List<string> FavoriteOpenedScreens
        {
            get
            {
                return this.favoriteClosedScreenListener.FavoriteOpenedScreens;
            }
        }

        public List<string> RecentlyOpenedScreens
        {
            get
            {
                return this.recentlyOpenedScreenListener.RecentlyOpenedScreens;
            }
        }

        public List<string> RecentlyClosedScreens
        {
            get
            {
                return this.recentlyClosedScreenListener.RecentlyClosedScreens;
            }
        }

        public void NavigateToWebPage(string webPage)
        {
            Uri target = new Uri(webPage);
            System.Windows.Browser.HtmlPage.Window.Navigate(target, "");
        }

        void innerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.OnPropertyChanged(e.PropertyName);
        }

        public bool CanNavigateForward
        {
            get
            {
                return this.selectedScreenNavigator.CanNavigateForward;
            }
        }

        public void NavigateForward()
        {
            this.selectedScreenNavigator.NavigateForward();
        }

        public bool CanNavigateBackward
        {
            get
            {
                return this.selectedScreenNavigator.CanNavigateBackward;
            }
        }

        public void NavigateBackward()
        {
            this.selectedScreenNavigator.NavigateBackward();
        }

        public bool NavigateTo(string screen)
        {
            return this.screenOpener.NavigateTo(screen);
        }


        public bool NavigateTo(string entityName, string dataSourceName, params object[] singleOrDefaultQueryArguments)
        {
            return this.entityOpener.NavigateTo(entityName, dataSourceName, singleOrDefaultQueryArguments);
        }

        public bool NavigateTo(string entityName, Microsoft.LightSwitch.IDataService dataService, params object[] singleOrDefaultQueryArguments)
        {
            return this.entityOpener.NavigateTo(entityName, dataService, singleOrDefaultQueryArguments);
        }
    }
}